var e = require("../../../@babel/runtime/helpers/interopRequireDefault"), t = require("../../../@babel/runtime/helpers/defineProperty"), a = e(require("../../../utils/util.js")), s = require("../common.js"), n = getApp();

Page({
    behaviors: [ s ],
    data: {
        memberId: "",
        memberData: {},
        memberChild: {},
        titleBgMode: "hide",
        rechargePopup: !1,
        typeText: "",
        popText: "",
        recharge: {
            integral: "",
            balance: "",
            remarks: ""
        },
        recommendId: "",
        recommendInfo: {},
        couponPopup: !1,
        CouponAll: [],
        ChooseCouponPopup: !1,
        info: {},
        couponsName: "",
        chooseCouponValue: "",
        coupons: {
            id: "",
            name: "",
            number: "",
            emptyName: !1
        },
        isEmitLevel: !1,
        level_list: [],
        member_list: [],
        memberFlag: !0,
        currentDate: new Date().getTime(),
        minDate: new Date("1990-1-1").getTime(),
        formatter: function(e, t) {
            return "year" === e ? "".concat(t, "年") : "month" === e ? "".concat(t, "月") : t;
        },
        count_dot: 1
    },
    onLoad: function(e) {
        e.id && (this.setData({
            memberId: e.id
        }), this.getData(), this.getAllCoupon(), this.getMemberData());
    },
    getMemberData: function() {
        var e = this;
        n._postNetWork({
            url: n.getNetAddresss("plugin.shop-assistant.frontend.member.get-group"),
            success: function(t) {
                1 == t.data.result && e.setData({
                    member_list: t.data.data
                });
            }
        });
    },
    changeMember_groupId: function(e) {
        this.setData(t({}, "memberChild.group_id", e.detail));
    },
    SearchRecommend: function(e) {
        this.setData({
            recommendId: e.detail.value
        }), this.throttleFn("recommend");
    },
    getMemberInfo: function() {
        var e = this;
        this.setData({
            memberFlag: !1
        }), n._postNetWork({
            url: n.getNetAddresss("plugin.shop-assistant.frontend.member.search-member"),
            data: {
                parent_id: this.data.recommendId
            },
            success: function(t) {
                1 == t.data.result && 0 !== t.data.data.length ? e.setData({
                    recommendInfo: t.data.data[0],
                    memberFlag: !0
                }) : e.setData({
                    recommendInfo: {},
                    memberFlag: !1
                });
            }
        });
    },
    onClick: function(e) {
        var t = e.currentTarget.dataset.info;
        this.setData({
            info: t,
            ChooseCouponPopup: !1
        });
    },
    setCouponsName: function(e) {
        var t = e.detail.value, a = this.data.CouponAll.filter(function(e) {
            if (-1 !== e.name.indexOf(t)) return e;
        });
        "" !== t ? this.setData({
            CouponAll: a
        }) : this.getAllCoupon();
    },
    getAllCoupon: function() {
        var e = this;
        n._postNetWork({
            url: n.getNetAddresss("plugin.shop-assistant.frontend.coupon.all-coupon"),
            success: function(t) {
                1 == t.data.result && e.setData({
                    CouponAll: t.data.data.list
                });
            }
        });
    },
    onInput: function(e) {
        this.setData({
            currentDate: e.detail
        });
    },
    setIsEmitLevel: function(e) {
        if (e) {
            var t = e.currentTarget.dataset.text;
            this.setData({
                typeText: t
            });
        }
        this.setData({
            isEmitLevel: !this.data.isEmitLevel
        });
    },
    onChangeCoupon: function(e) {
        this.setData({
            chooseCouponValue: e.detail
        });
    },
    changeLevel_id: function(e) {
        var a = e.detail;
        this.setData(t({}, "memberChild.level_id", a));
    },
    getlevel_name: function(e) {
        for (var a = this.data.level_list, s = 0; s < a.length; s++) {
            var n = a[s];
            if (e === n.id) return this.setData(t({}, "memberChild.level", n));
        }
    },
    validity: function() {
        var e = new Date(this.data.currentDate), t = new Date(), a = e.getTime() - t.getTime(), s = Math.floor(a / 864e5);
        return -1 == s ? 1 : 0 == s ? 2 : s < -1 ? "" : s + 2;
    },
    emitLevel: function() {
        var e = this;
        this.setIsEmitLevel();
        var t = "", a = {};
        "推荐人" == this.data.typeText ? (t = n.getNetAddresss("plugin.shop-assistant.frontend.member.change-parent"), 
        a = {
            uid: this.data.memberId,
            arent_id: this.data.recommendId
        }) : (t = n.getNetAddresss("plugin.shop-assistant.frontend.member.update-member"), 
        a = {
            id: this.data.memberId,
            member: {
                level_id: this.data.memberChild.level_id || "",
                validity: this.validity(),
                group_id: this.data.memberData.yz_member.group_id || ""
            }
        }), n._postNetWork({
            url: t,
            data: a,
            success: function(t) {
                var s = t.data, i = s.result, r = s.msg;
                if (1 != i) return n.tips(r);
                n.tips("修改成功"), "推荐人" !== e.data.typeText && e.getlevel_name(a.member.level_id), 
                e.getData();
            }
        });
    },
    showRechargePopup: function(e) {
        var t = e.currentTarget.dataset.type, a = {
            rechargePopup: !0,
            recharge: {
                integral: "",
                balance: "",
                remarks: ""
            }
        };
        a.popText = "integral" == t ? "积分" : "余额", this.setData(a);
    },
    closeRechargePop: function() {
        this.setData({
            rechargePopup: !1
        });
    },
    setRechargeContent: function(e) {
        var a = e.currentTarget.dataset.name;
        if ("balance" == a || "integral" == a) {
            for (var s = e.detail.value.replace(/[^0-9.-]/gi, ""), n = 1, i = 0, r = 0; r <= s.length; r++) "." == s[r] && (n += 1, 
            i = s.indexOf("."));
            if (n >= 2) {
                var o = s.split(".").join("");
                s = o = o.substring(0, i) + "." + o.substring(i);
            }
            "-" == s[0] && (s = "-" + (s = s.replace(/\-/gi, ""))), this.setData(t({}, "recharge.".concat(a), s));
        } else this.data.recharge[a] = e.detail.value;
    },
    setCouponsContent: function(e) {
        var t = e.currentTarget.dataset.name;
        "id" == t ? (this.setData({
            "coupons.id": e.detail.value
        }), this.throttleFn()) : this.data.coupons[t] = e.detail.value;
    },
    getData: function() {
        var e = this, t = n.getNetAddresss("plugin.shop-assistant.frontend.member.get-detail");
        n._postNetWork({
            url: t,
            data: {
                id: this.data.memberId
            },
            success: function(t) {
                var s = t.data;
                if (1 != s.result) return n.tips(s.msg);
                e.setData({
                    memberData: s.data,
                    memberChild: s.data.yz_member ? s.data.yz_member : {},
                    "memberData.createtime": a.default.formatTimeTwo(s.data.createtime, "Y-M-D h:m:s"),
                    level_list: s.data.level_list || []
                });
            }
        });
    },
    changeAgent: function(e) {
        var t = this;
        this.setData({
            "memberChild.is_agent": e.detail
        });
        var a = n.getNetAddresss("plugin.shop-assistant.frontend.member.update-agent");
        n._postNetWork({
            url: a,
            data: {
                id: this.data.memberId,
                is_agent: this.data.memberChild.is_agent
            },
            success: function(a) {
                if (1 != a.data.result) return t.setData({
                    "memberChild.is_agent": 1 == e.detail ? 0 : 1
                }), n.tips("修改失败");
                n.tips("修改成功");
            }
        });
    },
    confirmRecharge: function() {
        "积分" === this.data.popText ? this.rechargeIntegral() : this.rechargeBalance();
    },
    rechargeIntegral: function() {
        var e = this;
        if (!this.data.recharge.integral) return n.tips("请输入充值数量");
        var t = {};
        t.uid = this.data.memberId, t.integral = {
            integral: 1 * this.data.recharge.integral,
            remark: this.data.recharge.remarks
        };
        var a = n.getNetAddresss("plugin.shop-assistant.frontend.member.recharge-integral");
        n._postNetWork({
            url: a,
            data: t,
            success: function(t) {
                var a = t.data;
                n.tips(a.msg), 1 == a.result && (e.closeRechargePop(), e.getData());
            }
        });
    },
    rechargeBalance: function() {
        var e = this;
        if (!this.data.recharge.balance) return n.tips("请输入充值数量");
        var t = {};
        t.uid = this.data.memberId, t.balance = {
            balance: 1 * this.data.recharge.balance,
            remark: this.data.recharge.remarks
        };
        var a = n.getNetAddresss("plugin.shop-assistant.frontend.member.recharge-balance");
        n._postNetWork({
            url: a,
            data: t,
            success: function(t) {
                var a = t.data;
                n.tips(a.msg), 1 == a.result && (e.closeRechargePop(), e.getData());
            }
        });
    },
    showChooseCoupon: function() {
        this.setData({
            ChooseCouponPopup: !0
        });
    },
    showCouponPopup: function() {
        this.setData({
            couponPopup: !0,
            coupons: {
                id: "",
                name: "",
                number: "",
                emptyName: !1
            }
        });
    },
    throttleFn: function() {
        var e = this, t = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : "conpon";
        this.timeoutId && clearTimeout(this.timeoutId), this.timeoutId = setTimeout(function() {
            "conpon" == t ? e.searchConpon() : e.getMemberInfo();
        }, 800);
    },
    searchConpon: function() {
        var e = this, t = n.getNetAddresss("plugin.shop-assistant.frontend.coupon.coupon-detail");
        n._postNetWork({
            url: t,
            data: {
                id: this.data.coupons.id
            },
            success: function(t) {
                var a = t.data;
                if (1 != a.result) return e.setData({
                    "coupons.emptyName": !0
                }), n.tips(a.msg);
                e.setData({
                    "coupons.emptyName": !1,
                    "coupons.name": a.data.name
                });
            }
        });
    },
    confirmGive: function() {
        var e = this;
        if (this.data.info && !this.data.info.name) return n.tips("请选择优惠劵");
        if (!this.data.coupons.number) return n.tips("请输入赠送优惠券数量");
        var t = {
            uid: this.data.memberId,
            id: this.data.info.id,
            send_total: 1 * this.data.coupons.number
        }, a = n.getNetAddresss("plugin.shop-assistant.frontend.coupon.send-coupon");
        n._postNetWork({
            url: a,
            data: t,
            success: function(t) {
                var a = t.data;
                n.tips(a.msg), 1 == a.result && (e.setData({
                    couponPopup: !1
                }), e.getData());
            }
        });
    },
    closeCouponPop: function(e) {
        var a = e.currentTarget.dataset.type;
        this.setData(t({}, a, !1));
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onPageScroll: function(e) {
        e.scrollTop >= 50 && "hide" == this.data.titleBgMode ? this.setData({
            titleBgMode: "show"
        }) : e.scrollTop <= 50 && "show" == this.data.titleBgMode && this.setData({
            titleBgMode: "hide"
        });
    },
    setTopping: function() {
        wx.pageScrollTo({
            scrollTop: 0,
            duration: 300
        });
    },
    onShareAppMessage: function() {}
});